package com.aipolice.web;

import com.aipolice.pojo.ResponseInfo;
import com.aipolice.pojo.User;
import com.aipolice.service.UserService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import static com.aipolice.utils.sms.SmsUtil.checkPhoneLogin;

@RestController
public class LoginController {

    @Autowired
    UserService userService;
    ResponseInfo responseInfo = new ResponseInfo();
    @GetMapping("/phonelogin/{phone}/{code}")
    public ResponseInfo phonelogin(@PathVariable String phone, @PathVariable String code){
        System.out.println(userService.findByPhone(phone));
        User user = userService.findByPhone(phone);
        responseInfo.setCode("200");
        responseInfo.setData(new Gson().toJson(user));
        System.out.println(responseInfo);

        //本来是要检查的，Mob公司该规则了，太麻烦了
//        if (checkPhoneLogin( phone, code)){
//            System.out.println(userService.findByPhone(phone));
//            User user = userService.findByPhone(phone);
//            responseInfo.setCode("200");
//            responseInfo.setData(new Gson().toJson(user));
//            System.out.println(responseInfo);
//        }else{
//            responseInfo.setCode("400");
//            responseInfo.setData("");
//        }
        return responseInfo;
    }

    @GetMapping("/passwordlogin/{username}/{password}")
    public ResponseInfo passwordlogin(@PathVariable String username, @PathVariable String password){
        System.out.println(userService.findByUsernameAndPassword(username,password));
        User user = userService.findByUsernameAndPassword(username,password);
        if(null!=user){
            responseInfo.setCode("200");
            responseInfo.setData(new Gson().toJson(user));
            System.out.println(responseInfo);
        }
        else{
            responseInfo.setData("");
            responseInfo.setCode("400");
        }

        //本来是要检查的，Mob公司该规则了，太麻烦了
//        if (checkPhoneLogin( phone, code)){
//            System.out.println(userService.findByPhone(phone));
//            User user = userService.findByPhone(phone);
//            responseInfo.setCode("200");
//            responseInfo.setData(new Gson().toJson(user));
//            System.out.println(responseInfo);
//        }else{
//            responseInfo.setCode("400");
//            responseInfo.setData("");
//        }
        return responseInfo;
    }
}
